package nuc.edu.tlias.controller;

import lombok.extern.slf4j.Slf4j;
import nuc.edu.tlias.pojo.Clazz;
import nuc.edu.tlias.pojo.PageResult;
import nuc.edu.tlias.pojo.Result;
import nuc.edu.tlias.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    ClazzService clazzService;
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        clazzService.add(clazz);
        return Result.success();
    }

    @DeleteMapping("/{id}")

    public Result deleteById(@PathVariable Integer id){
        log.info("删除班级id：{}",id);

        clazzService.deleteById(id);

        return Result.success();
    }

    @GetMapping
    public Result serchPage( String name ,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin ,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                             @RequestParam(defaultValue = "1") Integer page ,
                             @RequestParam(defaultValue = "10")Integer pageSize){


        log.info("{},{},{},{},{}",name,begin,end,page,pageSize);
        PageResult pageResult = clazzService.serchPage(name,begin,end,page,pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Clazz>list= clazzService.findAll();

        log.info("------------------------{}",list);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result upload(@RequestBody Clazz clazz){

        clazzService.upload(clazz);
        return Result.success();
    }

}
